package com.linjiang.design.strategy.v2;

/**
 * 策略模式改造僵尸游戏
 * 定义了算法族，分别封装起来，让它们之间可以相互替换，此模式的变化独立于算法的使用者
 */
public class StrategyTest {
    public static void main(String[] args) {
        NormalZombie normalZombie = new NormalZombie();
        normalZombie.display();
        normalZombie.move();
        normalZombie.attack();

        //赋予不同的能力
        normalZombie.setAttackAble(new HeadAttack());
        normalZombie.attack();
    }
}

abstract class Zombie{

    //抽象方法，具体由子类实现
    abstract void display();

    //移动的能力
    MoveAble moveAble;

    //攻击的能力
    AttackAble attackAble;

    public Zombie(MoveAble moveAble, AttackAble attackAble) {
        this.moveAble = moveAble;
        this.attackAble = attackAble;
    }

    abstract void move();
    abstract void attack();

    public MoveAble getMoveAble() {
        return moveAble;
    }

    public void setMoveAble(MoveAble moveAble) {
        this.moveAble = moveAble;
    }

    public AttackAble getAttackAble() {
        return attackAble;
    }

    public void setAttackAble(AttackAble attackAble) {
        this.attackAble = attackAble;
    }
}

interface MoveAble{
    void move();
}

interface AttackAble{
    void attack();
}

class StepByStepMove implements MoveAble{

    @Override
    public void move() {
        System.out.println("一步一步移动。");
    }
}

class SimpleAttack implements AttackAble{

    @Override
    public void attack() {
        System.out.println("普通攻击");
    }
}

class HeadAttack implements AttackAble{

    @Override
    public void attack() {
        System.out.println("头部攻击");
    }
}


//普通僵尸
class NormalZombie extends Zombie{

    public NormalZombie() {
        //调用父类构造方法，传入默认能力实现
        super(new StepByStepMove(), new SimpleAttack());
    }

    @Override
    public void display() {
        System.out.println("我是普通僵尸");
    }

    @Override
    void move() {
        //调用具体的能力实现
        moveAble.move();
    }

    @Override
    void attack() {
        //调用具体的能力实现
        attackAble.attack();
    }
}



